/**
 * 
 * @since  2009/3/3
 * @author ashen
 * 
 * Description: 处理管理员对资源的权限设置的操作。
 * 
 */

	
	var Privilege={
		
		//设置资源操作权限的表单地址
 		$formUrl: "adminPage/setPrivilege.jsp",
 		
 		//设置资源操作权限的表单中每一项资源的表格地址
 		$tableUrl: "adminPage/operation.jsp",
		
		//存储返回的权限级别的权限信息
		$privilegeXml: "",
		
		//存储返回的资源类型列表
		$typeXml: "",
		
		//权限级别ID
		$roleId:0,
		
		//当前操作的资源类型
		$tempType: "",
		
		//当前权限级别下对资源的增删改查权限值
		$getAction:0,
		$putAction:0,
		$deleteAction:0,
		$postAction:0,
		
		//权限级别管理中的设置权限的连接的单击事件
		firstShow : function(event){
			//获取权限级别的权限信息
			var id=$(event.target).parent().attr("id");
			Privilege.show(id);			
		},
		
		//显示资源管理界面
		show : function(id){
			$.get(AdminDo.host+"admin/setprivilege/random="+Math.random()+"/"+id,
				function(data){				
				Privilege.$privilegeXml=$(data).find("privileges");			
			});
			//获得资源类型列表			
			$.get(AdminDo.host+"admin/resourceType/show"+"?random="+Math.random(),
				function(data){
				Privilege.$typeXml=$(data).find("typelist");
				Privilege.loadForm();			
			});
			Privilege.$roleId=id;			
		},
		
		//加载增加资源类型的权限设置的表单
		loadForm : function(){
			$(AdminDo.$sectionRoot).empty();
			//获得权限级别名称
			var roleName=Privilege.$privilegeXml.find("name").text();
			var html="<table><th>"+roleName+"</th><tr>";
			html+="<td>资源名</td><td colspan=\"4\">设置操作权限</td></tr>";
			var type;
			Privilege.$typeXml.find("list > *").each(function(){
				type=$(this).attr("type");
				//加载每项资源的权限设置表格
				html+="<tr><td rowspan=\"2\">"+type+"</td>";
				html+="<td><a class=\"addOperation\">添加资源的权限</a></td>";
				html+="<td><a class=\"deleteOperation\">删除资源的权限</a></td>";
				html+="<td><a class=\"modifyOperation\">修改资源的权限</a></td>";
				html+="<td><a class=\"queryOperation\">查看资源的权限</a></td></tr>";
				html+="<tr><td id=\""+type+"\" class=\"newpanel\" colspan=\"4\"></td></tr>";
			});
			html+="</table>";
			//加载资源权限设置表单
			$(AdminDo.$sectionRoot).html(html);
			//绑定添加，删除，修改，查看链接的单击事件，单击后加载权限设置操作面板
			$(".addOperation").click(Privilege.showAdd);
			$(".deleteOperation").click(Privilege.showDelete);
			$(".modifyOperation").click(Privilege.showModify);
			$(".queryOperation").click(Privilege.showQuery);		
		},
		
		//获得触发单击事件的链接所属的资源的属性值
		getValue: function(event){
			var tempObject=$(event.target).parent().parent().next().find("td:eq(0)");
			if(tempObject.attr("id") !== undefined){
				Privilege.$tempType=tempObject.attr("id");
			}
			tempObject=Privilege.$privilegeXml.find('resourcetype[@type="'+Privilege.$tempType+'"]').parent();
			if(tempObject.attr("get") !== undefined){
				Privilege.$getAction=tempObject.attr("get");
			}else{
				Privilege.$getAction=0;
			}
			if(tempObject.attr("put") !== undefined){
				Privilege.$putAction=tempObject.attr("put");
			}else{
				Privilege.$putAction=0;
			}
			if(tempObject.attr("delete") !== undefined){
				Privilege.$deleteAction=tempObject.attr("delete");
			}else{
				Privilege.$deleteAction=0;
			}
			if(tempObject.attr("post") !== undefined){
				Privilege.$postAction=tempObject.attr("post");
			}else{
				Privilege.$postAction=0;
			}			
		},
		
		//当单选钮被选择，将改变值
		changeValue: function(event){
			if($(event.target).parent().attr("name")=='post'){
				Privilege.$postAction=$(event.target).val();
			}else if($(event.target).parent().attr("name")=='delete'){
				Privilege.$deleteAction=$(event.target).val();
			}else if($(event.target).parent().attr("name")=='put'){
				Privilege.$putAction=$(event.target).val();
			}else if($(event.target).parent().attr("name")=='get'){
				Privilege.$getAction=$(event.target).val();
			}
		},
		
		//加载修改资源权限的表单
		getForm : function(event,radio,name){
			Privilege.getValue(event);
			var innerHtml="<form name=\""+name+"\" method=\"post\">"+radio;
			innerHtml+="<input type=\"button\" id=\"submit\" name=\"submit\" value=\"修改\"/></form>";
			$(".newpanel *").remove();
			$(".newpanel").each(function(){
				if($(this).attr("id") == Privilege.$tempType){
					$(this).html(innerHtml);
				}
			});
			
			//根据当前权限级别的资源权限值初始化单选框
			if(name=="post"){						
				if(Privilege.$postAction!='0'){
					$('input[@value="1"]').attr({"checked":"checked"});				
				}else{
					$('input[@value="0"]').attr({"checked":"checked"});	
				}
			}else if(name=="delete"){
				if(Privilege.$deleteAction=='0'){
					$('input[@value="0"]').attr({"checked":"checked"});				
				}else if(Privilege.$deleteAction=='1'){
					$('input[@value="1"]').attr({"checked":"checked"});	
				}else{
					$('input[@value="2"]').attr({"checked":"checked"});
				}
			}else if(name=="put"){
				if(Privilege.$putAction=='0'){
					$('input[@value="0"]').attr({"checked":"checked"});				
				}else if(Privilege.$putAction=='1'){
					$('input[@value="1"]').attr({"checked":"checked"});	
				}else{
					$('input[@value="2"]').attr({"checked":"checked"});
				}
			}else{
				if(Privilege.$getAction!='0'){
					$('input[@value="1"]').attr({"checked":"checked"});				
				}else{
					$('input[@value="0"]').attr({"checked":"checked"});	
				}
			}
			
			//绑定单选钮click事件
			$('input[@type="radio"]').click(Privilege.changeValue);	
			//绑定表单提交事件
			$('#submit').click(Privilege.doModify);
		},
		
		//加载资源的添加权限设置面板
		showAdd: function(event){			
			var tempHtml="<label>允许添加该类资源</label>";
			tempHtml+="<input type=\"radio\" name=\"set\" value=\"1\"/><br/>";
			tempHtml+="<label>禁止添加该类资源</label>";
			tempHtml+="<input type=\"radio\" name=\"set\" value=\"0\"/><br/>";			
			Privilege.getForm(event,tempHtml,"post");
		},
		
		//加载资源的删除权限设置面板
		showDelete: function(event){			
			var tempHtml="<label>允许删除任何用户的该类资源</label>";
			tempHtml+="<input type=\"radio\" name=\"set\" value=\"2\"/><br/>";
			tempHtml+="<label>只允许删除用户自己创建的该类资源</label>";
			tempHtml+="<input type=\"radio\" name=\"set\" value=\"1\"/><br/>";
			tempHtml+="<label>禁止删除该类资源</label>";
			tempHtml+="<input type=\"radio\" name=\"set\" value=\"0\"/><br/>";
			Privilege.getForm(event,tempHtml,"delete");
		},
		
		//加载资源的修改权限设置面板
		showModify: function(event){
			var tempHtml="<label>允许修改任何用户的该类资源</label>";
			tempHtml+="<input type=\"radio\" name=\"set\" value=\"2\"/><br/>";
			tempHtml+="<label>只允许修改用户自己创建的该类资源</label>";
			tempHtml+="<input type=\"radio\" name=\"set\" value=\"1\"/><br/>";
			tempHtml+="<label>禁止修改该类资源</label>";
			tempHtml+="<input type=\"radio\" name=\"set\" value=\"0\"/><br/>";
			Privilege.getForm(event,tempHtml,"put");
		},
		
		//加载资源的查询权限设置面板
		showQuery: function(event){
			var tempHtml="<label>允许查看用户自己创建的该类资源</label>";
			tempHtml+="<input type=\"radio\" name=\"set\" value=\"1\"/><br/>";
			tempHtml+="<label>禁止查看该类资源</label>";
			tempHtml+="<input type=\"radio\" name=\"set\" value=\"0\"/><br/>";			
			Privilege.getForm(event,tempHtml,"get");
		},
		
		//修改当前权限级别下的资源的权限值
		doModify:function(){
			$(AdminDo.$sectionRoot).empty();	
			$.ajax({
			type: "PUT",
			url: AdminDo.host + "admin/setprivilege/modify",
			data: Privilege.$roleId+","+Privilege.$tempType+","+Privilege.$getAction+","+Privilege.$putAction+","+Privilege.$deleteAction+","+Privilege.$postAction,
			success: function(data){
				alert(data);
				Privilege.show(Privilege.$roleId);
			},error:function(){
				alert(data);
				Privilege.show(Privilege.$roleId);
			}});	
		}
	
	};
	
	
	